'''
textur_startpage
'''
#encoding:utf-8
import os
import logging
import sys
from PIL import Image

reload(sys)
sys.setdefaultencoding('utf-8')

def _path_fmt(path):
    return path.replace("/", os.sep)

def resize_batch(texture, pageinfo):
    '''resize_batch'''
    img = Image.open(texture)

    logging.debug("img size =====>",img.size)

    for tarpath in pageinfo:
        info = pageinfo[tarpath]

        infolist = info.split(",")

        img_rotate = int(infolist[2])
        img_width = int(infolist[0 if img_rotate == 0 else 1])
        img_height = int(infolist[1 if img_rotate == 0 else 0])

        logging.debug("------------------start --------------------")

        logging.debug("img_rotate = %d img_width = %d img_height = %d"%(img_rotate, img_width, img_height))

        scale = float(float(img_width)/float(img.size[0]))

        resize_width = int(img.size[0]*scale)
        resize_height = int(img.size[1]*scale)

        logging.debug("resize_width = %d resize_height = %d"%(resize_width,resize_height))

        if resize_height < img_height:
            scale = float(float(img_height)/float(img.size[1]))
            resize_width = int(img.size[0]*scale)
            resize_height = int(img.size[1]*scale)

        both_img = img.resize((resize_width, resize_height), Image.ANTIALIAS)

        left_x = (both_img.size[0] - img_width)/2

        box = (left_x, both_img.size[1] - img_height, left_x+img_width, both_img.size[1])

        no_rotate_img = both_img.crop(box)

        logging.debug("no_rotate_img =",no_rotate_img.size)

        if img_rotate == 1:
            no_rotate_img = no_rotate_img.transpose(Image.ROTATE_270)

        logging.debug("rotate size =",no_rotate_img.size)

        path_base = os.path.split(tarpath)[0]
        if not os.path.isdir(path_base):
            os.mkdir(path_base)

        if os.path.isfile(tarpath + ".png"):
            os.remove(tarpath + ".png")

        no_rotate_img.save(tarpath + ".png")
        logging.debug("Resize the image [%s] to [%s]", texture, tarpath)

def pack_platpage(basetexture, topath, platform="ios"):
    '''pack_platicon'''
    if platform == "ios":
        sizemap = {"Default":"320,480,1",
                   "Default@2x":"640,960,1",
                   "Default~ipad":"768,1024,1",
                   "Default-568":"320,568,1",
                   "Default-568h":"568,320,0",
                   "Default-568h@2x":"640,1136,1",
                   "Default-667h@2x":"750,1334,1",
                   "Default-736h@3x":"1242,2208,1",
                   "Default-Landscape~ipad":"1024,768,0"}

        resizemap = {}

        for pagename in sizemap:
            resizemap[_path_fmt(topath + "/%s"%pagename)] = sizemap[pagename]
        resize_batch(basetexture, resizemap)

def pack_platgamepage(basetexture, tartexture, platform="ios"):
    '''pack_platgamepage'''
    if platform == "ios":
        Image.open(basetexture).save(tartexture)

if __name__ == "__main__":
    pack_platpage("/Volumes/backup/bra_jenkins_workspace/tools/BuildRelease/pagepath/page1/default.png","/Users/kaixin/Desktop/test_icon", platform="ios")
